package han.cirno.corrupt.compose

import android.os.Build
import android.util.Base64
import androidx.activity.compose.LocalActivity
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalContext
import androidx.hilt.navigation.compose.hiltViewModel
import han.cirno.corrupt.activity.main.MainActivityViewModel
import han.cirno.corrupt.components.monet.MonetPalette
import han.cirno.corrupt.components.monet.MonetPaletteSystem
import han.cirno.corrupt.compose.builtin_theme.CorruptColor
import han.cirno.corrupt.compose.builtin_theme.IotaColor
import han.cirno.corrupt.compose.builtin_theme.MikuColor
import han.cirno.corrupt.data.SettingColorTheme
import han.cirno.corrupt.util.SerialUtil
import kotlinx.coroutines.flow.map

@Composable
fun AppTheme(
    darkTheme: Boolean,
    legacy:Boolean,
    content: @Composable () -> Unit
) {
    val defaultMonetPalette=CorruptColor.defaultMonetPalette
    val mainActivityViewModel:MainActivityViewModel= hiltViewModel(LocalActivity.current as AppCompatActivity)
    val themeConfig by mainActivityViewModel.configColorThemeState.collectAsState()
    val pictureTheme by mainActivityViewModel.configPictureColorThemeState.map {
        return@map try {
                SerialUtil.deserialize(Base64.decode(it, Base64.DEFAULT), MonetPalette::class)
            }catch (_:Exception){defaultMonetPalette}
        }.collectAsState(defaultMonetPalette)
    val customTheme by mainActivityViewModel.configUserColorThemeState.map {
        return@map try {
                SerialUtil.deserialize(Base64.decode(it, Base64.DEFAULT), MonetPalette::class)
            }catch (_:Exception){defaultMonetPalette}
        }.collectAsState(defaultMonetPalette)
    val dynamicColor=themeConfig==SettingColorTheme.DYNAMIC_COLOR
    val pictureColor=themeConfig==SettingColorTheme.BACKGROUND_BASE
    val customColor=themeConfig==SettingColorTheme.CUSTOMIZE
    val builtIn= arrayOf(
        SettingColorTheme.BUILTIN_IOTA,
        SettingColorTheme.BUILTIN_MIKU)
    val colorScheme = darkSelect(when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            MonetPaletteSystem(
                dynamicLightColorScheme(context),
                dynamicDarkColorScheme(context))
        }
        pictureColor-> pictureTheme?: defaultMonetPalette
        customColor-> customTheme?: defaultMonetPalette
        themeConfig in builtIn ->{
            when(themeConfig){
                SettingColorTheme.BUILTIN_IOTA-> IotaColor.defaultMonetPalette
                SettingColorTheme.BUILTIN_MIKU->MikuColor.defaultMonetPalette
                else->defaultMonetPalette
            }
        }
        else -> defaultMonetPalette
    }.let { if(legacy) it.legacyPalette else it },darkTheme)
    MaterialTheme(
        colorScheme = colorScheme,
        content = content
    )
}

fun darkSelect(monetPalette: MonetPalette,darkTheme: Boolean):ColorScheme=
    if (darkTheme) monetPalette.darkScheme else monetPalette.lightScheme
